//
//  DepartmentListViewController.m
//  安好
//
//  Created by lianchuang on 14/11/11.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DepartmentListViewController.h"
#import "DepartmentTableViewCell.h"
@interface DepartmentListViewController ()

@end

@implementation DepartmentListViewController
{
    NSArray *departmentArray;
    NSDictionary *departmentData;
}
@synthesize sel_count;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"选择科室";
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
    departmentArray=[[NSArray alloc]init];
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.hidden=YES;
    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableview.frame=CGRectMake(0, 0,kScreenWidth , kScreenHeight-64);
    departmentData=[[NSDictionary alloc]init];
    [self loadDepartmentsData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    if (self.departmentBlock!= nil) {
        //某个事件触发，这时blocks里的内容就会运行
        self.departmentBlock(departmentData);
    }
}
//返回
- (void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma  mark  请求所有科室
/**
 *  请求标准科室
 */
- (void)loadDepartmentsData
{
    [self showActionView];
    NSString *isFromMyFocuse;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
     if (self.isFromBaike){
        isFromMyFocuse = [NSString stringWithFormat:@"wiki/hospitalDepartment"];
         
         [dict setObject:kPlatform forKey:@"platform"];
         [dict setObject:kVersion forKey:@"version"];
         [dict setObject:self.hosid forKey:@"hospitalid"];
         
         AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
         [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,isFromMyFocuse] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSNumber * statusNum = [responseObject objectForKey:@"status"];
             NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
             NSString * statusString = [numberFormatter stringFromNumber:statusNum];
             [self hiddenActionView];
             if ([statusString isEqualToString:@"0"]) {
                     [self shouLabel:@"暂无内容"];              
             }else{
            NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
                 
                 self.tableview.hidden=NO;
                 departmentArray=[jsonDictArr valueForKey:@"data"];
                 departmentData=[departmentArray objectAtIndex:0];

                [self.tableview reloadData];
             }
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             [self hiddenActionView];
         }];


    }else{
    
    NSString * requestURL = [NSString stringWithFormat:@"%@question/departmentdetail/?pid=0",kBaseURL];
    NSURL *url=[NSURL URLWithString:requestURL];
    AFHTTPRequestOperation *operation=[[AFHTTPRequestOperation alloc]initWithRequest:[NSURLRequest requestWithURL:url]];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSString *status=[NSString stringWithFormat:@"%@",[dict valueForKey:@"status"]];
        if ([status isEqualToString:@"1"]) {
            self.tableview.hidden=NO;
            departmentArray=[dict valueForKey:@"data"];
//            departmentData=[departmentArray objectAtIndex:0];
            for (int i=0; i<departmentArray.count; i++) {
                NSString *depar=[NSString stringWithFormat:@"%@",[[departmentArray objectAtIndex:i] valueForKey:@"departmentid"]];
                if ( [depar isEqualToString:sel_count]) {
                    departmentData=[departmentArray objectAtIndex:i];
                }
            }

            [self.tableview reloadData];
        }
        [self hiddenActionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    [operation start];
    }
}

#pragma mark  TableviewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return departmentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifer=@"department";
    DepartmentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell==nil) {
        if (kScreenWidth==414) {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"DepartmentTableViewCell" owner:self options:nil] lastObject];
        }else{
            cell=[[[NSBundle mainBundle] loadNibNamed:@"DepartmentTableViewCell" owner:self options:nil] objectAtIndex:0];
        }
    }
    cell.departmentName.text=[[departmentArray objectAtIndex:indexPath.row] valueForKey:@"departmentname"];
    NSString *departid=[NSString stringWithFormat:@"%@",[[departmentArray objectAtIndex:indexPath.row] valueForKey:@"departmentid"]];
    
    if ([sel_count isEqualToString:departid]) {
        cell.selectedImage.hidden=NO;
    }else
    {
        cell.selectedImage.hidden=YES;
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark TableviewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DepartmentTableViewCell *cell=(DepartmentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//    sel_count=indexPath.row;
    departmentData=[departmentArray objectAtIndex:indexPath.row];
    sel_count=[NSString stringWithFormat:@"%@",[[departmentArray objectAtIndex:indexPath.row] valueForKey:@"departmentid"]];
    [tableView reloadData];
    cell.selectedImage.hidden=NO;
    
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DepartmentTableViewCell *cell=(DepartmentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.selectedImage.hidden=YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(kScreenWidth==414)
    {
        return 53;
    }
    return 44.f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 块的方法
/**
 *  利用块进行页面传值
 *
 *  @param block 块
 */
- (void)returnDepartmentName:(DepartmentBlock)block
{
    self.departmentBlock=block;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
